﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;

namespace Commands.TorrentDownloaders
{
    // TODO
    public class AnimeLayerDownloadDispatcher : TokenDispatcherBase, ITokenDispatcher
    {

        public AnimeLayerDownloadDispatcher(/*ILog log*/)
            : base()
        {
        }

        protected override NameValueCollection GetLoginPasswordData()
        {
            return new NameValueCollection
            {
                { "username", ConfigurationManager.AppSettings["animelayer_login"] },
                { "password", ConfigurationManager.AppSettings["animelayer_password"] },
                { "returnto", "" }
            };
        }

        protected override string LoginIndicator()
        {
            return String.Format(
                ">{0}</span></b>", ConfigurationManager.AppSettings["animelayer_login"]).ToLower();
        }

        //protected override bool TryGetTorrent(string url, int maxTries)
        //{
        //    int cnt = maxTries;
        //    bool success = false;
        //    do
        //    {
        //        cnt--;

        //        try
        //        {
        //            //log.Write("Trying to download from " + url);

        //            var bytes_response = client.DownloadData(url);
        //            string response = Encoding.UTF8.GetString(bytes_response);

        //            Regex regex = new Regex(@"href=""(?<download_inner_link>/download\.php\?id=\d+\&amp;name=.animelayer\.ru._\d+\.torrent)""><b>", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
        //            Match m = regex.Match(response);
        //            if (m.Success)
        //            {
        //                string download_inner_link = m.Groups["download_inner_link"].Value;
        //                string download_link = "http://animelayer.ru" + download_inner_link.Replace("&amp;", "&");
        //                downloadToFile(download_link, url);
        //                success = true;
        //            }
        //            else
        //            {
        //                //log.Write("torrent download link not found");
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            //log.Write("Exception: " + ex.Message);
        //        }
        //        if (!success)
        //            Thread.Sleep(ONE_MINUTE);

        //    } while (!success && cnt > 0);
        //    return success;
        //}

        private void downloadToFile(string url, string referer)
        {
            string path = GetPath();

            client.Headers.Add("Referer", referer);
            client.DownloadFile(url, path);
            client.Headers.Remove("Referer");

            // Add torrent to torrent downloader.
            StartTorrent(path);

            //log.Write("Torrent file downloaded from " + url + " to " + path);
        }


        protected override Uri DispatcherLoginPage
        {
            get
            {
                throw new NotImplementedException();
                //return new Uri(@"http://login.rutracker.org/forum/login.php");
            }
        }



        protected override string DOWNLOAD_LINK_REGEXP
        {
            get
            {
                return "\"(?<download_link>http\\://dl\\.free-torrents\\.org/forum/dl\\.php\\?id=\\d\\d+)\"";
            }
        }

        protected override string CHECK_TRACKER_REGEXP
        {
            get { throw new NotImplementedException(); }
        }
    }
}
